package com.xms.schema.api.controller;

import com.xms.core.exception.XmsException;
import com.xms.schema.api.model.UpdateOptionSetModel;
import com.xms.schema.entity.OptionSet;
import com.xms.schema.entity.OptionSetDetail;
import com.xms.schema.service.optionset.IOptionSetFinderService;
import com.xms.schema.service.optionset.IOptionSetUpdaterService;
import com.xms.utils.CollectionUtil;
import com.xms.utils.StringUtil;
import com.xms.utils.UUIDUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * OptionSetUpdaterController
 * </p>
 *
 * @author migo
 * @since 2021/9/19
 */
@RestController
@RequestMapping("/{org}/api/schema/optionset")
public class OptionSetUpdaterController extends ApiBaseController {
    private IOptionSetUpdaterService _optionSetUpdaterService;
    private IOptionSetFinderService _optionSetFinderService;
    @Autowired
    public void OptionSetUpdaterController(IOptionSetFinderService optionSetFinderService,
                                           IOptionSetUpdaterService optionSetUpdaterService) {
        _optionSetFinderService = optionSetFinderService;
        _optionSetUpdaterService = optionSetUpdaterService;
    }

    @ApiOperation("修改选项集")
    @PostMapping("update")
    public JsonResultObject update(@RequestBody UpdateOptionSetModel model) throws XmsException, SQLException {
        if(UUIDUtil.isEmpty(model.optionSetId)){
            return JNotSpecifiedParams("optionsetid");
        }
        if(CollectionUtil.isEmpty(model.optionSetName)
                || CollectionUtil.isEmpty(model.optionSetValue)){
            return JNotSpecifiedParams("选项的名称与值");
        }
        OptionSet os = _optionSetFinderService.getById(model.optionSetId);
        os.setName(model.name);
        os.setDescription(model.description);
        List<OptionSetDetail> details = new ArrayList<>();
        int i = 0;
        for (String item : model.optionSetName)
        {
            if (StringUtil.isEmpty(item)) continue;
            OptionSetDetail osd = new OptionSetDetail();
            osd.setOptionsetid(os.getOptionsetid());
            osd.setOptionsetdetailid(UUIDUtil.get());
            osd.setName(item);
            osd.setValue(model.optionSetValue[i]);
            osd.setIsselected(model.isSelectedOption[i]);
            osd.setDisplayorder(i);
            details.add(osd);
            i++;
        }
        os.setItems(details);
        return _optionSetUpdaterService.update(os) ? JUpdateSuccess() : JUpdateFailure();
    }
}
